/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ctrl;

import entities.Room;
import hotel.jpa.Database;
import model.ViewRoomsBackingBean;
import hotel.jpa.IJpaCtrl;
import hotel.jpa.IRoomJpaCtrl;
import hotel.jpa.RoomJpaCtrl;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Arb
 */
@ManagedBean(name="viewRoomsControllerBean")
@ApplicationScoped
public class ViewRoomsControllerBean {
    
    protected List<Room> rooms = new ArrayList<Room>();
    protected Room r = null;
    
    public List<Room> getRooms(){
        
        //Get connection
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hotel_pu");
        IRoomJpaCtrl roomCtrl = new RoomJpaCtrl(null, emf);
      
        //Get all rooms
        rooms = roomCtrl.findEntities();

        return rooms;
    }

    public void setRooms(List<Room> r){
        this.rooms = r;
    }
    
      public Room getRoom(Long id) {
        rooms = getRooms();
        
        for(int i = 0; i < rooms.size(); i++) {
            if(rooms.get(i).getId() ==id) {
                r = rooms.get(i);
            }
        }
        
        return r;
        
    }
    
    public void removeRoom(Room r) {
        rooms = getRooms();

        if (rooms.contains(r)) {
            Database.getRoomController().destroy(r.getId());
            rooms.remove(r);
        }
    }
}
